package com.reguang.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.reguang.ManageApplication;
import com.reguang.pojo.SysMenuDO;
import com.reguang.pojo.TreeDO;
import com.reguang.result.Result;
import com.reguang.service.SysMenuService;
import com.reguang.utils.Tree;

import lombok.extern.slf4j.Slf4j;

/**
 * 菜单管理
 * 
 * @author 路汉腾
 *
 */
@RequestMapping("sys/menu")
@Controller
@Slf4j
public class SysMenuController {

	private String prefix = "system/menu";

	@Autowired
	private SysMenuService sysMenuService;

	@GetMapping()
	private String getMenuPage() {
		return prefix + "/menu";
	}

	/**
	 * 
	 * @Title: list
	 * @Description: 返回所有的菜单
	 * @param menu
	 * @return
	 * @return List<SysMenuDO>
	 */
	@GetMapping("/list")
	@ResponseBody
	public List<SysMenuDO> list(SysMenuDO menu) {
		return sysMenuService.selectListMenuInfo();
	}

	/**
	 * 
	* @Title: openAddMenuPage
	* @Description: 打开添加菜单页面
	* @param pId 父类id	
	* @param name 父类名称
	* @param model
	* @return
	* @return String
	 */
	@GetMapping("/add/{pId}/{name}")
	public String openAddMenuPage(@PathVariable String pId,@PathVariable String name, ModelMap model) {
		model.put("pName", name);
		model.put("pId", pId);
		return prefix + "/add";
	}
	/**
	 * 
	* @Title: openEditMenuPage
	* @Description: 打开修改菜单页面
	* @param id
	* @param model
	* @return
	* @return String
	 */
	@GetMapping("/edit/{id}")
	public String openEditMenuPage(@PathVariable String id ,ModelMap model) {
		SysMenuDO  menuDO= sysMenuService.selectMenuInfo(id);
	    model.put("menuDO",JSON.toJSONString(menuDO));
		return prefix + "/edit";
	}
	/**
	 * 
	* @Title: saveUpdateMenuInfo
	* @Description: 保存菜单修改
	* @param menuDo
	* @return
	* @return Result
	 */
	@PostMapping("/update")
	@ResponseBody
	public Result saveUpdateMenuInfo(SysMenuDO menuDo) {
		if(sysMenuService.editMenuInfo(menuDo)>0) {
			return Result.success();
		}
		return Result.failure();
	}
	
	/**
	 * 
	* @Title: save
	* @Description: 保存新增菜单
	* @param menuDo
	* @return
	* @return Result
	 */
	@ResponseBody
	@PostMapping("/save")
	public Result saveMenuInfo(SysMenuDO menuDo) {
		if(sysMenuService.saveMenuInfo(menuDo)>0) {
			return Result.success();
		}
		return Result.failure();
	}
	/**
	 * 
	* @Title: removeMenu
	* @Description: 删除菜单
	* @param menuDo
	* @return
	* @return Result
	 */
	@ResponseBody
	@PostMapping("/remove")
	public Result removeMenu(SysMenuDO menuDo) {
		if(sysMenuService.removeMenu(menuDo)>0) {
			return Result.success();
		}
		return Result.failure();
	}
	/**
	 * 
	* @Title: getListTreeById
	* @Description: 获取权限菜单
	* @return
	* @return List<TreeDO>
	 */
	@GetMapping("/tree")
	@ResponseBody
	public List<TreeDO> getListTreeById(){
		return	sysMenuService.selectListTree();
	}
	@GetMapping("/roleMenu/{roleId}")
	@ResponseBody
	public Integer[] getRoleMenuRelation(@PathVariable Integer roleId) {
		return sysMenuService.selectRoleMenuRelation(roleId);
	}
	

}
